﻿using System;

using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class SurchargeNames : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblAirline.Text = Session["AIRLINE"].ToString();
            if (!IsPostBack)
            {
                if (Request.QueryString["surcharge_code"] == null)
                {
                    lblError.Text = "Bad page calling!";
                }
                lblSurchargeCode.Text = Request.QueryString["surcharge_code"];
            }
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please check the record which you want to delete!";
                return;
            }
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            txtName.Text = txtName.Text.Trim();
            try
            {
                cmd.CommandText = "bll_surcharge_delete_name";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", lblSurchargeCode.Text);
                para = cmd.Parameters.AddWithValue("@lang", Request.Form["KEY"]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Deleting failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            txtName.Text = txtName.Text.Trim();
            try
            {
                cmd.CommandText = "bll_surcharge_add_name";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", lblSurchargeCode.Text);
                para = cmd.Parameters.AddWithValue("@lang", lstLang.SelectedValue);
                para = cmd.Parameters.AddWithValue("@name", txtName.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Adding failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }
    }
}